package pl.plesken.xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedList;

import noNamespace.CategoriesDocument;
import noNamespace.CategoriesType;
import noNamespace.CategoryType;

import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;

public class CategoriesXML {
	
	LinkedList<String> row = new LinkedList<String>();
	LinkedList<String> save = new LinkedList<String>();
	StringBuffer str = new StringBuffer();
	StringBuffer xml = new StringBuffer();

	public LinkedList<String> getRow() {
		return row;
	}

	public void setRow(LinkedList<String> row) {
		this.row = row;
	}

	public LinkedList<String> getSave() {
		return save;
	}

	public void setSave(LinkedList<String> save) {
		this.save = save;
	}

	public void getCategoryValues() {
		try {
			File xmlFile = new File("categories.xml");
			CategoriesDocument load = CategoriesDocument.Factory.parse(xmlFile);
			
			CategoryType[] category = load.getCategories().getCategoryArray();
			
			row.clear();
			
			for (int x = 0; x < category.length; x++) {
				if (!category[x].getName().equals("")) {
					str.append(category[x].getName());
				} else {
					str.append("empty");
				}
				if (!category[x].getEnable().equals("")) {
					str.append(";" + category[x].getEnable());
				} else {
					str.append(";empty");
				}
				if (!category[x].getRename().equals("")) {
					str.append(";" + category[x].getRename());
				} else {
					str.append(";empty");
				}
				String val = str.toString();
				row.add(val);
				str.setLength(0);
			}
			
		} catch(XmlException e) {
			System.err.println(e);
		} catch(IOException e) {
			System.err.println(e);
		} catch(NullPointerException e) {
		}
	}
	
	/*public void saveCategoryValues() {
		try {
			FileOutputStream cfg = new FileOutputStream("categories.xml"); //$NON-NLS-1$
			BufferedWriter cfgxml = new BufferedWriter(new OutputStreamWriter(cfg));
			
			xml.setLength(0);
			
			xml.append("<categories>\n");
			
			for (int i = 0; i < save.size(); i++) {
				String[] tab = save.get(i).split(";");
				if (!tab[0].toString().equals("null")) {
					if (tab[0].toString().equals("empty")) {
						xml.append("  <category name=\"\">\n");
					} else {
						xml.append("  <category name=\"" + tab[0].toString() + "\">\n");
					}
					if (tab[2].toString().equals("empty")){
						xml.append("    <enable>false</enable>\n");
					} else {
						xml.append("    <enable>" + tab[2].toString() + "</enable>\n");
					}
					if (tab[1].toString().equals("empty")) {
						xml.append("    <rename></rename>\n");
					} else {
						xml.append("    <rename>" + tab[1].toString() + "</rename>\n");
					}
					xml.append("  </category>\n");
				}
			}

			xml.append("</categories>\n");
			
			cfgxml.write(xml.toString());
			cfgxml.close();
			save.clear();

		} catch(FileNotFoundException e) {
			
		} catch(IOException e) {
			
		} catch(NoSuchElementException e) {
			
		}
	}*/
	
	public void saveCategoryValues() {
		try {
			CategoriesDocument categories = CategoriesDocument.Factory.newInstance();
			CategoriesType categoriesType = CategoriesType.Factory.newInstance();
		
			for (int y = 0; y < save.size(); y++) {
				String[] tab = save.get(y).split(";");
			
				if (!tab[0].toString().equalsIgnoreCase("null")) {
					categoriesType.addNewCategory();
					CategoryType categoryType = CategoryType.Factory.newInstance();
					categoryType.setName(tab[0].replace("empty", ""));
					categoryType.setEnable(tab[2].replace("empty", ""));
					categoryType.setRename(tab[1].replace("empty", ""));
					categoriesType.setCategoryArray(y, categoryType);
				}
			}
		
			categories.setCategories(categoriesType);
		
			save.clear();
		
			File out = new File("categories.xml");
			OutputStream os = new FileOutputStream(out);
	
			XmlOptions options = new XmlOptions();
			options.setSavePrettyPrint();
			categories.save(out, options);
			os.flush();
			
		} catch(IOException e) {
			System.err.println(e);
		} catch(NullPointerException e) {
		}
	}
}
